#pragma strict
//var aliens:GameObject[];
var aliens = new Array();
var inactiveAliens = new Array();
static var speed: float = .04;
var edgeReached: boolean = false;
//static var speed: float = 1;

function Start () {
	fillArray();
}
function Update(){
	if(aliens.length >0){
		for(var alien:GameObject in aliens){
			if(alien.transform.position.x > 25 || alien.transform.position.x < -25){
				if(!edgeReached){
					edgeReached = true;
					speed *= -1.015;
					for(var alien2:GameObject in aliens){
						alien2.transform.position.y -= 1;
					}
				
				}
				else {
					return;
				}
			}
			if(alien.transform.position.y <= -5){
				print("you lose!");
				speed = 0;
			}
		}
		edgeReached = false;
	}
	else{
		reset();
	}
}

function fillArray() {
	aliens = GameObject.FindGameObjectsWithTag("Alien");
	print(aliens.length);
}

function removeAlien(alien: GameObject) {
	var AlienMovement: AlienMovment = alien.gameObject.GetComponent("AlienMovment");
	print(AlienMovement.pointValue);
	Score.score += AlienMovement.pointValue;
	aliens.Remove(alien);	
	alien.active = false;
	inactiveAliens.Add(alien);
	//Destroy(alien);		//destorys the alien that called this method
	
	
	//destroy doesn't take place until the end of the update loop, so we need to wait until then to fill the array
	//yield WaitForEndOfFrame;		 
	//fillArray();
}

function reset(){
	for(var alien:GameObject in inactiveAliens){
		alien.active = true;
		var AlienMovement: AlienMovment = alien.GetComponent("AlienMovment");
		alien.transform.position.x = AlienMovement.xStart;
		alien.transform.position.y = AlienMovement.yStart;
		var AlienCollision: AlienCollision = alien.GetComponent(AlienCollision);
		//var AlienCollision: AlienCollision = GameObject.Find("AlienPrefab").GetComponent(AlienCollision);
    	AlienCollision.targets = true;
		
	}
	Score.score += 1000;
	fillArray();

}